var app = getApp();
import api from '../../../utils/api.js'
import { HTTP } from '../../../utils/http.js'
import utils from '../../../utils/util.js'
import { config } from '../../../utils/config'

let http = new HTTP()

Page({
	data: {
		imgUrl: config.imgUrl,
		num: 1
	},
	onLoad: function (options) {
		let id = options.id
		this.setData({
			id: id
		})

		this.getOne(id); //获取详情
		this.getPidCookbooks(id); //获取推荐菜谱

	},
	getOne(id) {
		let that = this;
		let address = wx.getStorageSync('address');
		let data = {
			lat: address.latitude,
			lng: address.longitude,
			id: id
		}
		this.getInfo(api.getOne, 'GET', data).then(res => {
			res.details = JSON.parse(res.details)
			that.setData(res);
			wx.setNavigationBarTitle({
				title: res.name,
			})
		})
	},
	/*推荐菜谱*/
	getPidCookbooks(id) {
		let per_page = 1000;
		this.getInfo(api.getPidCookbooks, 'GET', { id, per_page }).then((res) => {
			this.setData({
				recomCook: res,
			})
		})
	},
	/*查看推荐菜谱*/
	showVideo(e) {
		let id = e.currentTarget.dataset.id
		wx.navigateTo({
			url: "/pages/video/video?id=" + id
		})
	},
	/*获取*/
	getInfo(url, method, data) {
		return new Promise((resolve, reject) => {
			wx.showLoading({
				title: '加载中...',
			})
			http.request({
				url: url,
				method: method,
				data: data,
				success: res => {
					if (res.msg && res.error_code) {
						wx.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1500
						});
						return;
					}
					if (res.code == 200) {
						resolve(res.data);
					}
					wx.hideLoading()
				},
				fail: () => { },
				complete: () => {
					wx.hideLoading()
				}


			})
		})
	},
	/*数量加减*/
	editNums(e) {
		let vageType = e.currentTarget.dataset.type;
		console.log(vageType);

		if (vageType === 'add') {
			console.log(this.data.num)
			this.setData({
				num: this.data.num + 1
			})
		}
		if (vageType === 'sub') {
			if (this.data.num > 1) {
				this.setData({
					num: this.data.num - 1
				})
			}
		}
	},
	addShopcar() {
		let data = {
			products: [{ product_id: this.data.id, num: this.data.num, shop_id: this.data.dockingshop_id[0] }]
		}
		this.getInfo(api.shoppingCartAdd, 'POST', data).then((res) => {
			wx.showToast({
				title: '添加菜篮成功',
				icon: 'success',
				duration: 1500
			})
			setTimeout(()=>{
				wx.navigateBack({
					complete: (res) => {},
				})
			}, 1500)
		})

	},
	/*搜索*/
	searchInput: utils.debounce(function (e) {
		let inputValue = e[0].detail.value;
		if (inputValue) {
			this.setData({
				inputValue,
				deleteBtn: true
			})
		}
	}, 200),
	/*删除搜索框内容*/
	deleteSearch() {
		console.log(1);

		this.setData({
			inputValue: '',
			deleteBtn: false
		})
	},
	/*搜索按钮*/
	searchHandle() {
		if (this.data.inputValue == '') {
			wx.showToast({
				title: '请输入菜品名称',
				icon: 'none',
				duration: 1500
			})
		}

	}



})